import requests

from lxml import etree
from tqdm import tqdm


def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    response.encoding = response.apparent_encoding
    return response


def save(novel_name, title, content):
    """
    :param novel_name:
    :param title:
    :param content:
    :return:
    """
    file_name = f"{novel_name}.txt"
    # 一定要记得加后缀.txt mode 保存方式 a是追加保存 encoding是保存编码
    with open(file_name, mode='a', encoding='utf-8') as f:
        # 写入标题
        f.write(title)
        # 写入换行
        f.write('\n')
        # 写入小说内容
        f.write(content)


def get_one_novel(name, novel_url):
    # 小说章节内容获取 #调用请求网页数据函数
    response = get_response(novel_url)
    # 构建解析对象
    tree = etree.HTML(response.text)
    # 小说标题
    title = tree.xpath('//div[@class="bookname"]/h1/text()')[0]
    # 小说内容 返回的是列表
    content_list = tree.xpath('//*[@id="content"]//text()')
    content_str = ''.join(content_list)
    save(name, title, content_str)


def get_all_url(html_url):
    # 调用请求网页函数 获取到的是某个小说的首页
    response = get_response(html_url)
    # 解析我们需要的小说章节标题 章节url内容
    # 构成解析对象
    tree = etree.HTML(response.text)
    # 首页的所有章节url
    all_url = tree.xpath('//*[@id="list"]/dl/dd/a/@href')
    # 小说的名字
    novel_name = tree.xpath('//*[@id="info"]/h1/text()')
    for url in tqdm(all_url):
        novel_url = 'https://www.xbiquge.la/' + url
        # 给get第一页函数传入参数 小说名字和链接
        get_one_novel(novel_name, novel_url)


if __name__ == '__main__':
    novel_id = input('输入书名ID：格式：/15/15409/')
    url = f'https://www.xbiquge.la{novel_id}'
    get_all_url(url)
















